package utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class GetMyIp {

	
	public String SOName;
	public String ip;



	public GetMyIp()
	{
		this.SOName =   System.getProperty("os.name");

		switch (this.SOName) {
		case "Windows 7":
			this.SetIpWindows7();
			break;
			
		case "Linux":
			this.SetIpWindows7();
			break;
		

		default:
			break;
		}





	}


	private void SetIpWindows7()
	{

		try {

			Process p = Runtime.getRuntime().exec("ipconfig");
			String s;

			BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
			
			// read the output from the command
			while ((s = stdInput.readLine()) != null) {
				if(s.contains("local") && s.contains("Adaptador") && s.contains("Ethernet"))
				{
					String line = stdInput.readLine();
					while(!line.contains("IPv4"))
					{
						line = stdInput.readLine();

					}
					this.setIp(line);
					break;
				}

			}
			
		}
		catch (IOException e) {
			System.out.println("exception happened - here's what I know: ");
			e.printStackTrace();

		} 
	}

	public String getIp()
	{
		return this.ip;
	}

	private void setIp(String line)
	{
		String str[]  = line.split(":");
		String ip = str[str.length -1].trim();
		this.ip = ip;

	}






}
